package com.dream.technology.components.expresion.imp;

import com.dream.technology.components.context.FlowContext;
import com.dream.technology.components.expresion.ExpressionService;
import com.dream.technology.db.entity.RcOperationalSymbol;
import com.dream.technology.db.entity.vo.RiskItemVo;
//import com.dream.technology.zycashLog.Console;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 描述：多选值比较 民族
 *
 * @author chen_q_i@163.com
 * 2018/12/6 : 14:06.
 * @version : 1.0
 */
@Slf4j
@Service("checkboxExpressionAnalysis")
public class CheckboxExpressionAnalysis implements ExpressionService {

    @Autowired
    private Map<String, RcOperationalSymbol> symbolMap;

    /**
     * 解析每个元素 ， 生成不同的表达式并计算 返回boolean 值
     *
     * @param flowContext
     * @param item
     * @return
     */
    @Override
    public boolean expressionAnalysis(FlowContext flowContext, RiskItemVo item) {

        // 构建表达式
        return buildExpression(flowContext, item);
    }


    /**
     * 构建表达式
     *
     * @param flowContext
     * @param item
     * @return
     */
    boolean buildExpression(FlowContext flowContext, RiskItemVo item) {
        // 构建表达式
        String symbolCode = item.getOperationalSymbolCode();
        String fieldCode = item.getFieldCode();
        String fieldValue = item.getFieldValue();
        String o =String.valueOf( flowContext.get(fieldCode));
        boolean contains=false;
        //包含
        boolean equals = "regex".equals(symbolMap.get(symbolCode).getValue());
        if (equals) {
            contains= fieldValue.contains(o);
        } else {
            //不包含
            contains= !fieldValue.contains(o);
        }
        String s = equals ? "" : "不";
        log.info("执行节点 ：{} 表达式_"+ s+"包含 ：{} 结果 ：{}",item.getFieldName(),o, contains);

        return contains;
    }
}
